/// [Integer right triangles](https://projecteuler.net/problem=40)

fn main() {
    let sum = (0..=6).map(|n| nth(10_u32.pow(n))).collect::<Vec<_>>();
    println!("{:?}", sum);
    println!("{:?}", sum.iter().fold(1, |s, x| s * x));
}

fn nth(n: u32) -> u32 {
    let mut d = 1;
    let mut n = n;
    while n > d * 9 * 10_u32.pow(d - 1) {
        n -= d * 9 * 10_u32.pow(d - 1);
        d += 1;
    }
    let num = 10_u32.pow(d - 1) + (n - 1) / d;
    if d == 1 {
        num
    } else {
        let digit = (d - (n - 1) % d) as u32;
        (num % 10_u32.pow(digit)) / 10_u32.pow(digit - 1)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn is_work() {
        assert_eq!(nth(1), 1);
        assert_eq!(nth(5), 5);
        assert_eq!(nth(11), 0);
        assert_eq!(nth(12), 1);
        assert_eq!(nth(13), 1);
    }
}
